using System.Collections;
using I2.Loc;
using UnityEngine;

public class WeaponSlot : EventCommander, ISlot
{
	public BaseWeapon weapon;

	public UITexture texWeapon;

	public UILabel ammoLabel;

	private bool selected;

	private Coroutine loadWeapon;

	private Coroutine loadTextureCoroutine;

	public UITexture Texture
	{
		get
		{
			return texWeapon;
		}
	}

	public Transform slotTransform
	{
		get
		{
			return base.gameObject.transform;
		}
	}

	public string elementName
	{
		get
		{
			return weaponName;
		}
	}

	public string slotName
	{
		get
		{
			return base.name;
		}
	}

	public string weaponName
	{
		get
		{
			if (weapon != null)
			{
				return weapon.name;
			}
			return string.Empty;
		}
	}

	public EventCommander eventCommander
	{
		get
		{
			return this;
		}
	}

	public float interfaceAnimationTime
	{
		get
		{
			return weapon.ReloadTime;
		}
	}

	public override void Init()
	{
		base.Init();
		InitEvents(new string[9] { "SlotSellected", "WeaponLoaded", "WeaponActivated", "WeaponUnloaded", "TextureLoaded", "ValueChanged", "WeaponVisiableChange", "AlternativeText", "StartInterfaceAnimation" });
	}

	public void SetActive(bool val)
	{
		selected = val;
		if (weapon != null && loadWeapon == null && loadTextureCoroutine == null)
		{
			activateWeapon(val);
		}
	}

	private void activateWeapon(bool val)
	{
		if (weapon == null || weapon.gameObject.activeSelf == val || loadWeapon != null || loadTextureCoroutine != null)
		{
			return;
		}
		if (val && texWeapon != null)
		{
			InitWeaponInterface(weapon);
			weapon.RemoveAllEventListeners(this);
			weapon.AddEventListener(this, "ChangeAmmo", delegate
			{
				showCountBulletCurrentWeapon();
			}, false);
			weapon.AddEventListener(this, "NoAmmo", delegate
			{
				OnEmpty(true);
			}, false);
			weapon.AddEventListener(this, "NoAmmoOnStart", delegate
			{
				OnEmpty(false);
			}, false);
			weapon.AddEventListener(this, "Reload", delegate
			{
				if (!weapon.SaveBullet)
				{
					RiseEvent(this, "StartInterfaceAnimation", interfaceAnimationTime, true);
				}
			}, false);
			weapon.AddEventListener(this, "ChangeSpread", delegate(Object sender, object[] args)
			{
				WalkWindow.instance.aimHUD.Shot((float)args[0]);
			}, false);
		}
		if (val)
		{
			Transform[] componentsInChildren = weapon.GetComponentsInChildren<Transform>(true);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				if (componentsInChildren[i] != weapon.transform)
				{
					componentsInChildren[i].gameObject.SetActive(true);
				}
			}
		}
		weapon.gameObject.SetActive(val);
		if (val)
		{
			RiseEvent(this, "SlotSellected", this);
			RiseEvent(this, "WeaponActivated", weapon);
		}
	}

	public void OnEmpty(bool val)
	{
		string text = "No ammo";
		text = ScriptLocalization.Get("Game_common/no_ammo");
		RiseEvent(this, "AlternativeText", true, text, val);
	}

	public void showCountBulletCurrentWeapon()
	{
		string text = ((weapon.showAmmoType == ShowAmmoType.NoAmmo) ? string.Empty : ((weapon.showAmmoType != ShowAmmoType.SeparateAmmo) ? (weapon.chargerAmmo + weapon.ammo).ToString() : (weapon.chargerAmmo + "/" + weapon.ammo)));
		RiseEvent(this, "AlternativeText", false, string.Empty, true);
		RiseEvent(this, "ValueChanged", text);
		if (weapon.AllBullet == 0 && weapon.showAmmoType != 0)
		{
			OnEmpty(false);
		}
	}

	public void NewWeapon(string newWeaponName, string defaultWeaponName, bool loadTexture = false)
	{
		if (weaponName != newWeaponName)
		{
			UnLoadWeapon();
			if (!(newWeaponName != string.Empty))
			{
				return;
			}
			if (loadTexture)
			{
				base.gameObject.SetActive(false);
				if ((bool)base.transform.parent.parent)
				{
					base.transform.parent = base.transform.parent.parent;
				}
			}
			base.gameObject.SetActive(true);
			if (loadWeapon != null)
			{
				StopCoroutine(loadWeapon);
			}
			string text = newWeaponName;
			if (Device.isWeakDevice && !settings.offlineMode && !loadTexture && !string.IsNullOrEmpty(defaultWeaponName))
			{
				text = defaultWeaponName;
			}
			loadWeapon = StartCoroutine(LoadWeapon(text, loadTexture));
		}
		else if (newWeaponName != string.Empty)
		{
			RiseEvent(this, "WeaponLoaded", weapon);
		}
	}

	public void UnLoadWeapon()
	{
		if (weapon != null)
		{
			weapon.RemoveWeapon();
			RiseEvent(this, "WeaponUnloaded", weapon);
		}
		if ((bool)texWeapon)
		{
			texWeapon.mainTexture = null;
			texWeapon = null;
		}
	}

	private IEnumerator LoadWeapon(string name, bool loadTexture)
	{
		if (!loadTexture)
		{
			yield return new WaitForSeconds(Random.Range(0.1f, 2f));
		}
		ResourceRequest newWeapon = Resources.LoadAsync<GameObject>("Weapons/" + name);
		yield return newWeapon;
		loadWeapon = null;
		if (weapon != null)
		{
			UnLoadWeapon();
		}
		if (newWeapon != null && newWeapon.asset != null)
		{
			GameObject weaponObj = Object.Instantiate(newWeapon.asset as GameObject);
			weapon = weaponObj.GetComponent<BaseWeapon>();
			weaponObj.transform.parent = base.transform;
			weaponObj.SetActive(false);
			yield return null;
			if (loadTexture)
			{
				if (loadTextureCoroutine != null)
				{
					StopCoroutine(loadTextureCoroutine);
				}
				loadTextureCoroutine = StartCoroutine(LoadAndAddTexture(weapon));
			}
			else
			{
				activateWeapon(selected);
			}
		}
		else
		{
			activateWeapon(false);
		}
		RiseEvent(this, "WeaponLoaded", weapon);
	}

	public void SetVisiable(bool val)
	{
		if (selected && weapon != null)
		{
			RiseEvent(this, "WeaponVisiableChange", val);
		}
	}

	public IEnumerator LoadAndAddTexture(BaseWeapon curWeapon)
	{
		ResourceRequest newTexture = Resources.LoadAsync<Texture>("AtlasGame/" + curWeapon.nameSpritePreview);
		yield return newTexture;
		loadTextureCoroutine = null;
		if (texWeapon == null)
		{
			texWeapon = base.gameObject.AddComponent<UITexture>();
		}
		texWeapon.depth = 20;
		texWeapon.mainTexture = newTexture.asset as Texture2D;
		WalkWindow.instance.slotSelector.SetSlot(slotName, this);
		if (base.gameObject.activeInHierarchy && base.gameObject.activeSelf)
		{
			StartCoroutine(SetSlotEnum());
		}
		base.transform.localRotation = Quaternion.identity;
		activateWeapon(selected);
		showCountBulletCurrentWeapon();
		RiseEvent(this, "TextureLoaded", texWeapon);
	}

	private IEnumerator SetSlotEnum()
	{
		yield return new WaitForSeconds(0.2f);
		WalkWindow.instance.slotSelector.SetSlot(slotName, this);
	}

	private void OnFinishCenterButtonWeapon(WeaponSlot curBut)
	{
		if (curBut == this)
		{
			RiseEvent(this, "SlotSellected", curBut);
		}
	}

	public void InitWeaponInterface(BaseWeapon weapon)
	{
		if (!weapon.isMobWeapon)
		{
			showCountBulletCurrentWeapon();
			WalkWindow.instance.butShoot.GetComponent<UISprite>().spriteName = weapon.nameSpriteShootBtn;
			if (weapon.mechanicType == MechanickType.Charging)
			{
				WalkWindow.instance.weaponStrange.Show();
			}
			else
			{
				WalkWindow.instance.weaponStrange.Hide();
			}
			WalkWindow.instance.aimHUD.SetAimType(weapon.aimType);
		}
	}

	private void OnDestroy()
	{
		UnLoadWeapon();
	}
}
